<template>
  <div class="goods">
    <!-- 面包屑导航 -->
    <breadcrumb :breadcrumb="breadcrumb"></breadcrumb>
    <!-- 内容卡片区 -->
    <el-card class="box-card content">
      <!-- 搜索及添加 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="query"
            class="input-with-select"
            clearable
            @clear="clearInp"
          >
            <el-button @click="serach" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button @click="addGoods" type="primary">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="goodTableData" style="width: 100%" border>
        <el-table-column type="index" label="#" width="50"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="110"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80"></el-table-column>
        <el-table-column label="创建时间" width="200">
          <template v-slot="scope">{{scope.row.upd_time |timestampToTime}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template v-slot="scope">
            <el-button @click="editGoods(scope.row)" size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button @click="delGoods(scope.row)" size="mini" type="danger" icon="el-icon-delete"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页控件区域 -->

      <div class="goods_block">
        <el-pagination
          @current-change="getGoodsList"
          @size-change="getGoodsList"
          :current-page.sync="pagenum"
          :page-sizes="[5, 10,15,20]"
          :page-size.sync="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalpage"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>
           
<script>
export default {
  name: "goods",
  data() {
    return {
      // 面包屑导航传值
      breadcrumb: ["商品管理", "商品列表"],
      // 用户输入内容
      query: "",
      // 当前页码
      pagenum: 1,
      // 每页显示条数
      pagesize: 5,
      totalpage: 0,
      // 商品列表数据
      goodTableData: []
    };
  },
  methods: {
    //初始化----获取商品列表数据
    async getGoodsList() {
      const { data: res } = await this.$http.get("/goods", {
        params: {
          query: this.query,
          pagenum: this.pagenum,
          pagesize: this.pagesize
        }
      });
      // 赋值数据
      this.goodTableData = res.data.goods;
      // 赋值当前页
      this.pagenum = res.data.pagenum - 0;
      // 赋值总页数
      this.totalpage = res.data.total - 0;
    },
    //清除输入框
    clearInp() {
      this.query = "";
      this.getGoodsList();
    },
    //点击搜索
    serach() {
      // 判断输入框是否有内容
      if (this.query.trim() == "") {
        return this.$notify({
          title: "警告",
          message: "输入的内容为空！",
          type: "warning",
          position: "top-left"
        });
      } else {
        this.getGoodsList();
      }
    },
    //添加商品
    addGoods() {
      // 添加编程式导航
      this.$router.push("/goods/add");
    },
    editGoods(row) {
      // console.log(row);
    },
    delGoods(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(
            `goods/${row.goods_id}`
          );
          if (res.meta.status == 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getGoodsList();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  created() {
    // 声明周期函数---获取商品列表数据
    this.getGoodsList();
  },
  filters: {
    timestampToTime(time) {
      var date = new Date(time * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let Y = date.getFullYear() + "-";
      let M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      let D = date.getDate() + " ";
      let h = date.getHours() + ":";
      let m = date.getMinutes() + ":";
      let s = date.getSeconds();
      return Y + M + D + h + m + s;
    }
  }
};
</script>
<style lang='less' scoped>
.goods {
  width: 100%;
  height: 100%;
  .content {
    margin-top: 10px;
    .el-table {
      margin-top: 10px;
    }
    .goods_block {
      margin-top: 10px;
    }
  }
}
</style>